package com.dcjt.firehotcarpad.fragment


import android.graphics.BitmapFactory
import android.location.Location
import android.os.Bundle
import com.amap.api.maps.*
import com.amap.api.maps.model.*
import com.amap.api.navi.AMapNavi
import com.amap.api.navi.AMapNaviListener
import com.amap.api.navi.model.*
import com.aries.library.fast.basis.BasisFragment
import com.aries.library.fast.manager.LoggerManager
import com.dcjt.firehotcarpad.R
import com.dcjt.firehotcarpad.manager.DataManager
import com.google.gson.Gson


/**
 * Created by cj on 2021/1/20.
 * Email:codesexy@163.com
 * Function:
 * desc:
 */
abstract class BaseMapFragment : BasisFragment(), AMap.OnMyLocationChangeListener,
    AMapNaviListener, AMap.OnPolylineClickListener {


    protected var mapView: MapView ?= null
    protected var aMap: AMap ?= null
    protected lateinit var mUiSettings: UiSettings

    protected  var currentPosition: Location?= null

    private var isFirst = true

    protected lateinit var mAMapNavi: AMapNavi

    override fun initView(savedInstanceState: Bundle?) {
        mapView = findViewById<MapView>(R.id.mapView)!!
        mapView!!.onCreate(savedInstanceState)
        aMap = mapView!!.map
        mUiSettings = aMap!!.uiSettings

        mUiSettings.isZoomControlsEnabled = false

        mUiSettings.isMyLocationButtonEnabled = false

        mUiSettings.logoPosition = AMapOptions.LOGO_MARGIN_RIGHT

        val myLocationStyle = MyLocationStyle()
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER)
        // 设置边框的颜色
        myLocationStyle.strokeColor(resources.getColor(R.color.mainColor));
        // 设置边框的填充色
        myLocationStyle.radiusFillColor(resources.getColor(R.color.fillColor));
        myLocationStyle.myLocationIcon(
            BitmapDescriptorFactory.fromBitmap(
                BitmapFactory.decodeResource(
                    resources, R.drawable.navi_map_gps_locked
                )
            )
        );

        myLocationStyle.interval(5000 * 6)

        aMap!!.myLocationStyle = myLocationStyle

        aMap!!.isMyLocationEnabled = true

        aMap!!.isTrafficEnabled = true

        aMap!!.setOnMyLocationChangeListener(this)

        //导航相关设置
        mAMapNavi = AMapNavi.getInstance(requireContext().applicationContext)

        mAMapNavi.addAMapNaviListener(this)

        aMap!!.setOnPolylineClickListener(this)

    }


    override fun onMyLocationChange(location: Location?) {
        location?.let {
            //第一次定位成功后就不让定位后在移动到指定中心点了
            currentPosition = location
//            App.currentPosition = location
            if (isFirst) {
                val cameraPosition =
                    CameraPosition(LatLng(it.latitude, it.longitude), 15f, 0f, 0f)
                val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition)
                aMap!!.animateCamera(cameraUpdate)
                isFirst = false
            }

        }
    }


    /**
     * 方法必须重写
     */
    override fun onResume() {
        super.onResume()
        mapView!!.onResume()
    }

    /**
     * 方法必须重写
     */
    override fun onPause() {
        super.onPause()
        mapView!!.onPause()
    }

    /**
     * 方法必须重写
     */
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        mapView?.onSaveInstanceState(outState)
    }

    /**
     * 方法必须重写
     */
    override fun onDestroy() {
        super.onDestroy()
        mapView?.onDestroy()
    }


    /**
     * 移动当前位置
     */
    fun toMyLocation() {
        currentPosition?.let {
            var zoom = aMap!!.cameraPosition.zoom
            zoom += 1
            val cameraPosition =
                CameraPosition(LatLng(it.latitude, it.longitude), zoom, 0f, 0f)
            val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition)
            aMap!!.animateCamera(cameraUpdate)
        }
    }

    /**
     * 放大
     */
    fun zoomOut() {
        var zoom = aMap!!.cameraPosition.zoom
        zoom += 1
        if (zoom <= 19) {
            val zoomTo = CameraUpdateFactory.zoomTo(zoom)
            aMap!!.animateCamera(zoomTo)
        }
    }


    /**
     * 缩小
     */
    fun zoomIn() {
        var zoom = aMap!!.cameraPosition.zoom
        zoom -= 1;
        if (zoom >= 3) {
            val zoomTo = CameraUpdateFactory.zoomTo(zoom)
            aMap!!.animateCamera(zoomTo)
        }
    }


    override fun onInitNaviFailure() {

    }

    override fun onInitNaviSuccess() {
    }

    override fun onStartNavi(p0: Int) {
    }

    override fun onTrafficStatusUpdate() {
    }

    override fun onLocationChange(p0: AMapNaviLocation?) {
    }

    override fun onGetNavigationText(p0: Int, p1: String?) {
    }

    override fun onGetNavigationText(p0: String?) {
    }

    override fun onEndEmulatorNavi() {
    }

    override fun onArriveDestination() {
    }

    override fun onCalculateRouteFailure(p0: Int) {
    }

    override fun onCalculateRouteFailure(p0: AMapCalcRouteResult?) {
        LoggerManager.e("onCalculateRouteFailure_货车路径规划失败："+ Gson().toJson(p0))
    }

    override fun onReCalculateRouteForYaw() {
    }

    override fun onReCalculateRouteForTrafficJam() {
    }

    override fun onArrivedWayPoint(p0: Int) {
    }

    override fun onGpsOpenStatus(p0: Boolean) {}

    override fun onNaviInfoUpdate(p0: NaviInfo?) {}

    override fun updateCameraInfo(p0: Array<out AMapNaviCameraInfo>?) {}

    override fun updateIntervalCameraInfo(
        p0: AMapNaviCameraInfo?,
        p1: AMapNaviCameraInfo?,
        p2: Int
    ) {

    }

    override fun onServiceAreaUpdate(p0: Array<out AMapServiceAreaInfo>?) {
    }

    override fun showCross(p0: AMapNaviCross?) {
    }

    override fun hideCross() {
    }

    override fun showModeCross(p0: AMapModelCross?) {
    }

    override fun hideModeCross() {
    }

    override fun showLaneInfo(p0: Array<out AMapLaneInfo>?, p1: ByteArray?, p2: ByteArray?) {

    }

    override fun showLaneInfo(p0: AMapLaneInfo?) {

    }

    override fun hideLaneInfo() {

    }

    override fun onCalculateRouteSuccess(p0: IntArray?) {

    }

    override fun onCalculateRouteSuccess(p0: AMapCalcRouteResult?) {
        // 获取路线数据对象

        // 获取路线数据对象
        val naviPaths = AMapNavi.getInstance(requireContext().applicationContext).naviPaths
        // 绘制显示路径
    }

    override fun notifyParallelRoad(p0: Int) {

    }

    override fun OnUpdateTrafficFacility(p0: Array<out AMapNaviTrafficFacilityInfo>?) {

    }

    override fun OnUpdateTrafficFacility(p0: AMapNaviTrafficFacilityInfo?) {

    }

    override fun updateAimlessModeStatistics(p0: AimLessModeStat?) {

    }

    override fun updateAimlessModeCongestionInfo(p0: AimLessModeCongestionInfo?) {

    }

    override fun onPlayRing(p0: Int) {

    }

    override fun onNaviRouteNotify(p0: AMapNaviRouteNotifyData?) {

    }

    override fun onGpsSignalWeak(p0: Boolean) {

    }

    override fun onPolylineClick(polyline: Polyline?) {

    }
}